package net.learn2develop.SMSMessaging;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SMSRead extends ListActivity  {

	ArrayList<String> strList = new ArrayList<String>();
	ArrayList<String> encryptedMessages = new ArrayList<String>();
	String encryptedCode = "";
	final String  HEADER = "From :"; 
	
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      
      
      Uri uriSMSURI = Uri.parse("content://sms/inbox");
      Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);
      String sms = "";
      while (cur.moveToNext()) {
          strList.add(HEADER + cur.getString(2) + " : " + cur.getString(11));
          encryptedMessages.add(cur.getString(11));
      }
      
      setListAdapter(new ArrayAdapter<String>(this, R.layout.getsms, strList));
      ListView lv = getListView();
      lv.setTextFilterEnabled(true);
      
      lv.setOnItemClickListener(new OnItemClickListener() 
      	  {
    	    public void onItemClick(AdapterView<?> parent, View view,
    	        int position, long id) {
    	    	
    	      // When clicked, show a toast with the TextView text
    	    	String encryptedCodeDisplay = (String) ((TextView) view).getText();
    	    	encryptedCode = encryptedMessages.get(position);
    	      Toast.makeText(getApplicationContext(), encryptedCodeDisplay,
    	          Toast.LENGTH_SHORT).show();
    	      
    	      finish();
    	    }
    	  });
  }
  
  
  @Override
	public void finish() {
		Intent data = new Intent();
		data.putExtra(Constants.ENCRYPTED_RETURN_VALUE, encryptedCode);
		//data.putExtra("returnKey2", "You could be better then you are. ");
		setResult(RESULT_OK, data);
		super.finish();
	}
  
  
  
}
